package com.iplatform.iplatformuser.privilege.service.impl;

import com.iplatform.iplatformuser.common.CollectionUtil;
import com.iplatform.iplatformuser.framework.util.UniqueIDGenerator;
import com.iplatform.module.privilege.DwTargetBean;
import com.iplatform.iplatformuser.privilege.dao.PriMenuMapper;
import com.iplatform.iplatformuser.privilege.dao.TargetMapper;
import com.iplatform.iplatformuser.privilege.service.inter.ITargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>Title: TargetServiceImpl</p>
* <p>Description: </p>
* @author RayLLi
* @date 2018年8月23日
 */
@Service
public class TargetServiceImpl implements ITargetService {

	@Autowired
	public TargetMapper targetMapper;

	@Autowired
	public PriMenuMapper priMenuMapper;

	@Override
	public List<DwTargetBean> query(Map<String, String> formMap) {
		return targetMapper.query(formMap);
	}

	@Override
	public List<DwTargetBean> queryRoleTargets(Map<String, String> formMap) {
		return targetMapper.queryRoleTargets(formMap);
	}

	@Transactional
	public int insert(DwTargetBean dwTargetBean) {
		return targetMapper.insert(dwTargetBean);
	}

	@Transactional
	public int update(DwTargetBean dwTargetBean) {
		return targetMapper.update(dwTargetBean);
	}

	@Transactional
	public int delete(String deleteId) {
		return targetMapper.delete(deleteId);
	}

	@Transactional
	public void batchDelete(String[] deleteIds) {
		targetMapper.batchDelete(deleteIds);
	}

	@Override
	public String queryMenuIdByMenuLink(String menuLink) {
		return priMenuMapper.getMenuIdByLink(menuLink);
	}

	@Transactional
	public void selectTargets(String[] selectTargetId, String menuId, String createdBy) {
		targetMapper.deleteTargetByMenuId(menuId);

		Map<String, String> menuTargetMap = new HashMap<>(4);
		for (String targetId : selectTargetId) {
			menuTargetMap.put("MENU_TARGET_ID", UniqueIDGenerator.getUUID());
			menuTargetMap.put("TARGET_ID", targetId);
			menuTargetMap.put("MENU_ID", menuId);
			menuTargetMap.put("CREATED_BY", createdBy);

			targetMapper.selectTarget(menuTargetMap);
		}

	}

	private final String isSelectedTarget ="Y";

	@Transactional
	public void selectTarget(String targetId, String menuId, String createdBy) {
		String isMenuSelectTarget = targetMapper.isMenuSelectTarget(menuId, targetId);

		if (isSelectedTarget.equals(isMenuSelectTarget)) {
			targetMapper.deleteTarget(menuId, targetId);
		} else {
			Map<String, String> menuTargetMap = new HashMap<>(4);
			menuTargetMap.put("MENU_TARGET_ID", UniqueIDGenerator.getUUID());
			menuTargetMap.put("TARGET_ID", targetId);
			menuTargetMap.put("MENU_ID", menuId);
			menuTargetMap.put("CREATED_BY", createdBy);

			targetMapper.selectTarget(menuTargetMap);
		}
	}

	@Override
	public List<Map<String,String>> selectTargetType() {
		return targetMapper.selectTargetType();
	}

	@Override
	public List<DwTargetBean> querySelectedTargetByMenuId(Map<String, String> formMap) {
		return targetMapper.querySelectedTargetByMenuId(formMap);
	}

	@Override
	public List<DwTargetBean> queryNotSelectedTargetByMenuId(Map<String, String> formMap) {
		return targetMapper.queryNotSelectedTargetByMenuId(formMap);
	}


	@Transactional
	public void insertTargetMenu(Map<String, String> formMap) {
		targetMapper.deleteTargetMenu(formMap);

		String targetIds = formMap.get("targetIds");
		String menuId = formMap.get("menuId");

		List<Map<String, String>> mapList = new ArrayList<>();
		if(!StringUtils.isEmpty(targetIds)) {
			for(String targetId : targetIds.split(",")) {
				Map<String, String> map = CollectionUtil.getHashMap(4);
				map.put("menuTargetId", UniqueIDGenerator.getUUID());
				map.put("targetId", targetId);
				map.put("menuId", menuId);
				map.put("createBy", formMap.get("createBy"));
				mapList.add(map);
			}
			targetMapper.insertTargetMenu(mapList);
		}
	}



}
